package com.h3c.nem.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.h3c.nem.client.IttpClient;
import com.h3c.nem.dto.client.IttpClientDTO;
import com.h3c.nem.utils.AppBusinessException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Service
public class IttpClientService {

    @Resource
    IttpClient ittpClient;


    public HashMap<String, String> getPostDeptList(IttpClientDTO ittpClientDTO){
        try {
            String postDeptList = ittpClient.getIttpPostDeptList(ittpClientDTO);
            JSONObject jsonObject = JSONObject.parseObject(postDeptList);
            Object data = jsonObject.get("body");
            List<IttpClientDTO> dtos = JSONArray.parseArray(data.toString(), IttpClientDTO.class);
            HashMap<String, String> dept = new HashMap<>();
            for (IttpClientDTO dto : dtos) {
                if (!dept.containsKey(dto.getPostTypeName()  + ":" + dto.getPostTypeID())) {
                    dept.put(dto.getPostTypeName()  + ":" + dto.getPostTypeID(), dto.getPostName() + ":" + dto.getPostNameID());
                } else {
                    String temp = dept.get(dto.getPostTypeName() + ":" + dto.getPostTypeID());
                    dept.put(dto.getPostTypeName()  + ":" + dto.getPostTypeID(), temp + "," + dto.getPostName() + ":" + dto.getPostNameID());
                }
            }
            return dept;
        } catch (Exception e){
            throw new AppBusinessException("系统出错，请稍后重试");
        }
    }


}
